
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClientExample.Controls;
using ClientExample.Containers;
using FlowLib.Events;
using FlowLib.Containers;

namespace ClientExample.Client.Interface
{
    public partial class SearchResultWindow : Form
    {
        protected System.Windows.Forms.TabPage tabNoSearchResult = new System.Windows.Forms.TabPage();

        public new bool Close
        {
            get;
            set;
        }

        public SearchResultWindow()
        {
            Close = false;
			IntPtr h = Handle;  // This is for stupid mono stuff. Some how handle isnt created correctly if we dont do this :(
            InitializeComponent();

            // Add no message tab
            OneMessageControl noMsg = new OneMessageControl();
            noMsg.Text = "No new searches";
            noMsg.Dock = System.Windows.Forms.DockStyle.Fill;
            tabNoSearchResult.Controls.Add(noMsg);
            AddNoResults();

            tabControl1.MouseClick += new System.Windows.Forms.MouseEventHandler(tabControl1_MouseClick);
        }

        public void AddSearch(SearchInfo info)
        {
            SearchResultTab tab = new SearchResultTab(info);
            tabControl1.TabPages.Add(tab);
            RemoveNoResults();
        }

        public void AddHub(HubControl hub)
        {
            try
            {
                hub.HubUpdate += new FlowLib.Events.FmdcEventHandler(OnHubUpdate);
                //hubs.Add(hub.Id, hub);
            }
            catch (ArgumentException)
            {
                hub.HubUpdate -= OnHubUpdate;
                hub.Dispose();
                // TODO : Make a better information box
                System.Windows.Forms.MessageBox.Show(string.Format("You have duplicate connections to:{0} - {1}.\r\n One of the connections will be disconnected.", hub.Id, hub.TopLabel.Text));
            }
        }

        public void OnHubUpdate(object sender, FlowLib.Events.FmdcEventArgs e)
        {
            ClientExample.Controls.HubControl hub = sender as ClientExample.Controls.HubControl;
            if (hub == null)
                return;
            switch (e.Action)
            {
                case Actions.SearchResult:
                    SearchResultInfo sr = e.Data as SearchResultInfo;
                    if (sr == null)
                        return;
                    lock (tabControl1)
                    {
                        foreach (SearchResultTab tab in tabControl1.TabPages)
                        {
                            //if (!string.IsNullOrEmpty( sr.Token ))
                            //{
                            //    tab.AddResult(sr);
                            //}
                            /*else */if (sr.Info.ContainsKey(ContentInfo.VIRTUAL) && sr.Info.Get(ContentInfo.VIRTUAL).IndexOf(tab.Info.Get(SearchInfo.SEARCH), StringComparison.OrdinalIgnoreCase) != -1)
                            {
                                // TODO : Add more checks of search result here
                                tab.AddResult(sr);
                            }
                        }
                    }
                    break;
                case EventActions.Close:
                    hub.HubUpdate -= OnHubUpdate;
                    break;
            }
        }

        private void AddNoResults()
        {
            tabControl1.TabPages.Add(tabNoSearchResult);
        }

        private void RemoveNoResults()
        {
            tabControl1.TabPages.Remove(tabNoSearchResult);
        }
        
        private void SearchResultWindow_Load(object sender, EventArgs e)
        {

        }

        protected override void OnClosing(CancelEventArgs e)
        {
            //base.OnClosing(e);
            e.Cancel = !Close;
            if (!Close)
                this.Hide();
        }

        void tabControl1_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                lock (tabControl1.TabPages)
                {
                    for (int i = 0; i < tabControl1.TabCount; i++)
                    {
                        Rectangle rec = tabControl1.GetTabRect(i);
                        if (rec.Contains(e.Location))
                        {
                            tabControl1.TabPages.RemoveAt(i);
                            if (tabControl1.TabCount == 0)
                            {
                                AddNoResults();
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
